﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _FILE_ReadFileName
{
    class ReadFileList
    {
        /// <summary>
        /// Read All the files in the directory you given and subdirectory, 
        /// then return a List<string> contains all the file path and name.
        /// </summary>
        /// <param name="path">the directory you want to read</param>
        /// <param name="searchPattern">searchPattern like "*.js"</param>
        /// <returns></returns>
        public List<string> readFileName(string path, string searchPattern)
        {
            List<string> result = new List<string>();

            DirectoryInfo directory = new DirectoryInfo(path);
            DirectoryInfo[] subDirectory = directory.GetDirectories();
            FileInfo[] files = directory.GetFiles(searchPattern);
            foreach (FileInfo file in files)
            {
                result.Add(file.FullName);
            }

            foreach (DirectoryInfo subDir in subDirectory)
            {
                List<string> subResult = readFileName(subDir.FullName, searchPattern);
                result.AddRange(subResult);
            }

            return result;
        }
    }
}
